package com.ouman.luoliluoli.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ouman.luoliluoli.HotGirlsDetailActivity;
import com.ouman.luoliluoli.R;
import com.ouman.luoliluoli.models.HotGirlsModel;
import com.ouman.luoliluoli.utils.GlobalConstants;

import java.util.List;
import java.util.Random;

/**
 * Created by jintian on 12/05/2017.
 */

public class HotGirlsRecyclerViewAdapter extends RecyclerView.Adapter<HotGirlsRecyclerViewAdapter.ViewHolder> {


    private Context context;
    private List<HotGirlsModel> data;
    public HotGirlsRecyclerViewAdapter(Context context, List<HotGirlsModel> data){
        this.context = context;
        this.data = data;
    }

    public HotGirlsRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_hotgirls, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("===>>click!!!!!");
            }
        });

        return new HotGirlsRecyclerViewAdapter.ViewHolder(view);
    }

    public void onBindViewHolder(final HotGirlsRecyclerViewAdapter.ViewHolder holder, final int position){
        String title = data.get(position).getTitle();
        final String imageUrlString = new GlobalConstants().API_TNGOU_IMAGE_BASE + data.get(position).getImg();

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_card_show);
        holder.cardViewHotGirls.startAnimation(animation);

        Glide.clear(holder.imageViewGirl);
        Glide.with(holder.imageViewGirl.getContext())
                .load(imageUrlString)
                .asBitmap()
                .error(R.drawable.bk_beauty)
                .into(holder.imageViewGirl);

        holder.textViewTitle.setText(title);
        holder.cardViewHotGirls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), HotGirlsDetailActivity.class);
                intent.putExtra("likeCount", new Random().nextInt(87000));
                intent.putExtra("imageUrl", imageUrlString);
                intent.putExtra("title", data.get(position).getTitle());
                intent.putExtra("downloadCount", new Random().nextInt(34000));
                view.getContext().startActivity(intent);
                System.out.println(data.get(position).getId());

            }
        });

    }

    public int getItemCount(){
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textViewTitle;
        ImageView imageViewGirl;
        CardView cardViewHotGirls;
        public ViewHolder(View view){
            super(view);
            textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
            imageViewGirl = (ImageView) view.findViewById(R.id.imageViewGirl);
            cardViewHotGirls = (CardView) view.findViewById(R.id.cardViewHotGirls);
        }

    }
}